package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/**
 * 攻略目录资源控制器
 */
@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model){
        qo.setOrderBy("sc.sequence");
        model.addAttribute("pageInfo",strategyCatalogService.query(qo));
        model.addAttribute("strategies",strategyService.selectAll(null));
        return "strategyCatalog/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StrategyCatalog strategyCatalog){
        JsonResult jsonResult = new JsonResult();
        try {
            strategyCatalogService.saveOrUpdate(strategyCatalog);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作失败!");
        }
        return jsonResult;
    }

    @RequestMapping("selectByStrategyId")
    @ResponseBody
    public List selectByStrategyId(Long strategyId){
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.selectByStrategyId(strategyId);
        return strategyCatalogs;
    }

}
